﻿/*
Copyright (C) 2012 Laurent Tardif (laurent.tardif@persistentsas.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace org.codeplex.TestUtils
{


    /// <summary>
    /// Helper class to check results
    /// </summary>
    public class StringUtils
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="toBeChecked"></param>
        /// <returns></returns>
        public static Boolean CheckContains(String content, String toBeChecked)
        {
            Boolean answer = false;
            int pos = content.IndexOf(toBeChecked, 0);
            if (pos >= 0)
            {
                Console.WriteLine("We have an occurence at" + pos);
                answer = true;
            }
            return answer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="toBeChecked"></param>
        /// <returns></returns>
        public static Boolean CheckDoNotContains(String content, String toBeChecked)
        {
            Boolean answer = true;
            int pos = content.IndexOf(toBeChecked, 0);
            if (pos >= 0)
            {
                Console.WriteLine("We have an occurence at" + pos);
                answer = false;
            }
            return answer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="toBeChecked"></param>
        /// <param name="NbMacOccurence"></param>
        /// <returns></returns>
        public static Boolean CheckExactOccurence(String content, String toBeChecked, int NbMacOccurence)
        {
            Boolean answer = true;
            int pos = -1;
            int oldPos = -1;
            for (int i = 0; i < NbMacOccurence; i++)
            {
                pos++;
                //Console.WriteLine("trying to look after pos = " + pos);
                oldPos = pos;
                pos = content.IndexOf(toBeChecked, pos);
                if (pos == -1)
                {
                    Console.WriteLine("miss the " + i + "eme occurence after pos" + oldPos);
                    answer = false;
                }
            }
            if (pos >= 0)
            {
                //Console.WriteLine("trying to look after pos = " + pos);
                pos = content.IndexOf(toBeChecked, pos + 1);
                //Console.WriteLine("pos = " + pos);             
                if (pos != -1)
                {
                    Console.WriteLine("there is an extra occurence after pos" + pos);
                    answer = false;
                }
            }
            return answer;
        }
    }
}
